package com.txt.controller;

import com.txt.result.R;
import com.txt.service.UploadService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@Tag(name = "对象存储")
@RequestMapping("/cos")
public class UploadController {

    @Resource
    private UploadService uploadService;

    @PostMapping("/upload")
    @Operation(summary = "上传文件")
    public R<String> uploadFile(@RequestParam("file") MultipartFile multipartFile) throws IOException {

        String key = UUID.randomUUID() + "-" + multipartFile.getOriginalFilename();
        String url = uploadService.upload(multipartFile, key);

        return R.ok(url);
    }
}    